<template>
  <div class="chat-wrapper">
    <div class="chat-container">
      <div class="chat-header">
        客服聊天
      </div>
      <div class="chat-messages" ref="chatMessages">
        <div
          v-for="(message, index) in messages"
          :key="index"
          :class="{'sent': message.isSent, 'received': !message.isSent}"
          class="message"
        >
          {{ message.text }}
        </div>
      </div>
      <div class="chat-input">
        <input 
          v-model="newMessage" 
          @keydown.enter="sendMessage"
          type="text"
          placeholder="输入留言..." 
          :disabled="isSending"
        />
        <button @click="sendMessage" :disabled="isSending">发送</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      messages: [
        { text: "您好！有什么可以帮您的吗？", isSent: false },
        { text: "我有一个问题想咨询。", isSent: true },
      ],
      newMessage: "",
      isSending: false,  // 用于禁用按钮和输入框防止重复提交
    };
  },
  methods: {
    sendMessage() {
      if (this.newMessage.trim() !== "") {
        this.isSending = true;  // 正在发送状态
        this.messages.push({
          text: this.newMessage,
          isSent: true
        });
        this.newMessage = "";
        this.scrollToBottom();  // 发送消息后滚动到底部

        // 模拟客服的自动回复
        setTimeout(() => {
          this.messages.push({
            text: "这是自动客服回复。",
            isSent: false
          });
          this.scrollToBottom();  // 客服回复后滚动到底部
          this.isSending = false;  // 恢复发送状态
        }, 1000);
      }
    },

    // 滚动聊天框到底部
    scrollToBottom() {
      const chatMessages = this.$refs.chatMessages;
      chatMessages.scrollTop = chatMessages.scrollHeight;
    }
  },
  watch: {
    // 监听 messages 数组的变化，确保每次消息变化时自动滚动
    messages() {
      this.$nextTick(() => {
        this.scrollToBottom();
      });
    }
  }
};
</script>

<style scoped>
/* 新增的 chat-wrapper 用于居中布局 */
.chat-wrapper {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center;   /* 垂直居中 */
  height: 100vh;         /* 占满可视窗口高度 */
  background-color: #f0f2f5;
  margin: 0;             /* 防止默认的 margin 影响布局 */
  padding: 0;            /* 防止默认的 padding 影响布局 */
  box-sizing: border-box; /* 确保 box-sizing 为 border-box */
}

.chat-container {
  border: 1px solid #ccc;
  border-radius: 5px;
  width: 400px;
  max-height: 500px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #fff; /* 添加背景色以更清晰地分隔组件 */
}

.chat-header {
  background-color: #007bff;
  color: #fff;
  padding: 10px;
  text-align: center;
}

.chat-messages {
  flex: 1;
  padding: 10px;
  overflow-y: auto;
  background-color: #f7f7f7;
  word-wrap: break-word; /* 处理长文本换行 */
  word-break: break-word; /* 防止超长文本溢出 */
}

.message {
  padding: 10px;
  margin: 5px 0;
  border-radius: 5px;
  max-width: 70%;
}

.sent {
  background-color: #d1e7dd;
  align-self: flex-end; /* 右对齐发送的消息 */
  margin-left: auto;    /* 消息推到右边 */
}

.received {
  background-color: #f1f1f1;
  align-self: flex-start; /* 左对齐接收的消息 */
}

.chat-input {
  display: flex;
  padding: 10px;
  border-top: 1px solid #ccc;
}

.chat-input input {
  flex: 1;
  padding: 5px;
  border: 1px solid #ccc;
  border-radius: 3px;
  margin-right: 10px;
}

.chat-input button {
  background-color: #007bff;
  color: white;
  border: none;
  padding: 8px 15px;
  border-radius: 3px;
  cursor: pointer;
}

.chat-input button:hover {
  background-color: #0056b3;
}

.chat-input button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}
</style>
